﻿// opencvhello3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
 

#define ROWNUM 10
#define COLNUM 14
#define MAX_WIDTH 14
#define MAX_HIGH 10
#define MAX_WIDTH_EX 16
#define MAX_HIGH_EX 12
#define SLEEPTIME 40 // 点击间隔
//#define PAUSE
int b[MAX_WIDTH][MAX_HIGH]; //图片矩阵，0为空白，从11开始表示不同图片
int map[MAX_WIDTH_EX][MAX_HIGH_EX]; //图片矩阵 + 一圈0（方便外围连接）
char * filename = "cwllknm.bmp";
int type=0; //关卡
/*
typedef struct tagPOINT {
int x;
int y;
} POINT;*/

int Find(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX]);
//测试是否结束
bool testEmpty(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX]);
//测试两个点是不是能一条线连接
bool testLine(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX],POINT *ap1,POINT* ap2);
//测试一个折线
bool testcorner(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX],POINT *ap1,POINT* ap2);
//测试两个折线
bool testtwocorner(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX],POINT *ap1,POINT* ap2);
//根据图片，生成图片矩阵
void gen_map();
//根据关卡，移动图片矩阵元素
void move_map(int map[MAX_WIDTH_EX][MAX_HIGH_EX]);
//截图，保存成文件，问题：速度慢，用到MFC? afxwin.h?
void screen(char filename[])
{ 
	CDC *pdc; 
	pdc=CDC::FromHandle(GetDC(NULL)); 
	int BitPerPixel=pdc->GetDeviceCaps(BITSPIXEL); 
	int width=pdc->GetDeviceCaps(HORZRES); 
	int height=pdc->GetDeviceCaps(VERTRES); 
	//cout<<"当前屏幕色彩模式为"<<BitPerPixel<<"位色彩"<<endl 
	//	<<"屏幕宽度:"<<width<<endl 
	//	<<"屏幕高度:"<<height<<endl<<endl; 

	CDC memdc; 
	memdc.CreateCompatibleDC(pdc); 

	CBitmap memBitmap, *oldmembitmap; 
	memBitmap.CreateCompatibleBitmap(pdc,width,height); 

	oldmembitmap=memdc.SelectObject(&memBitmap); 
	memdc.BitBlt(0,0,width,height,pdc,0,0,SRCCOPY); 

	BITMAP bmp; 
	memBitmap.GetBitmap(&bmp); 

	FILE *fp=fopen(filename,"wb"); 

	BITMAPINFOHEADER bih={0}; 
	bih.biBitCount=bmp.bmBitsPixel; 
	bih.biCompression=BI_RGB; 
	bih.biHeight=bmp.bmHeight; 
	bih.biPlanes=1; 
	bih.biSize=sizeof(BITMAPINFOHEADER); 
	bih.biSizeImage=bmp.bmWidthBytes*bmp.bmHeight; 
	bih.biWidth=bmp.bmWidth; 

	BITMAPFILEHEADER bfh={0}; 
	bfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); 
	bfh.bfSize=bfh.bfOffBits+bmp.bmWidthBytes*bmp.bmHeight; 
	bfh.bfType=(WORD)0x4d42; 

	fwrite(&bfh,1,sizeof(BITMAPFILEHEADER),fp); 
	fwrite(&bih,1,sizeof(BITMAPINFOHEADER),fp); 
	byte *p=new byte[bmp.bmWidthBytes*bmp.bmHeight]; 
	GetDIBits(memdc.m_hDC,(HBITMAP)memBitmap.m_hObject,0,height,p,(LPBITMAPINFO) &bih,DIB_RGB_COLORS); 

	fwrite(p,1,bmp.bmWidthBytes*bmp.bmHeight,fp); 

	delete []p; 
	fclose(fp); 
	memdc.SelectObject(oldmembitmap); 
}

void move_map(int map[MAX_WIDTH_EX][MAX_HIGH_EX]){
	int i,j,k;
	if(type==3){ // 上下分离
		for(k=1;k<MAX_WIDTH_EX-1;k++){
			for(i=MAX_HIGH_EX/2-1;i>1;i--)
				for(j=1;j<i;j++){ //上
					if(map[k][j]==0)
					{
						int temp = map[k][j];
						map[k][j]=map[k][j+1];
						map[k][j+1]=temp;
					}
				}
				for(i=MAX_HIGH_EX/2;i<MAX_HIGH_EX-1;i++) // 下
					for(j=MAX_HIGH_EX-2;j>i;j--){
						if(map[k][j]==0)
						{
							int temp = map[k][j];
							map[k][j]=map[k][j-1];
							map[k][j-1]=temp;
						}
					}
		}
	}
	if(type==4){ // 左右分离
		for(k=1;k<MAX_HIGH_EX-1;k++){
			for(i=MAX_WIDTH_EX/2-1;i>1;i--) // 左
				for(j=1;j<i;j++){
					if(map[j][k]==0)
					{
						int temp = map[j][k];
						map[j][k]=map[j+1][k];
						map[j+1][k]=temp;
					}
				}
				for(i=MAX_WIDTH_EX/2;i<MAX_WIDTH_EX-1;i++) // 右
					for(j=MAX_WIDTH_EX-2;j>i;j--){
						if(map[j][k]==0)
						{
							int temp = map[j][k];
							map[j][k]=map[j-1][k];
							map[j-1][k]=temp;
						}
					}
			}
	}
	if(type==5){ // 向左看齐 good
		for(k=1;k<MAX_HIGH_EX-1;k++){
			for(i=MAX_WIDTH_EX-2;i>1;i--)
				for(j=1;j<i;j++){
					if(map[j][k]==0)
					{
						int temp = map[j][k];
						map[j][k]=map[j+1][k];
						map[j+1][k]=temp;
					}
				}
		}
	}
	if(type==6){ // 地心引力 bad
		for(k=1;k<MAX_WIDTH_EX-1;k++){
				for(i=1;i<MAX_HIGH_EX-1;i++)
					for(j=MAX_HIGH_EX-2;j>i;j--){
						if(map[k][j]==0)
						{
							int temp = map[k][j];
							map[k][j]=map[k][j-1];
							map[k][j-1]=temp;
						}
					}
		}
	}
	if(type==7){ // 飘向天空 good
		for(k=1;k<MAX_WIDTH_EX-1;k++){
			for(i=MAX_HIGH_EX-2;i>0;i--)
				for(j=1;j<i;j++){ //上
					if(map[k][j]==0)
					{
						int temp = map[k][j];
						map[k][j]=map[k][j+1];
						map[k][j+1]=temp;
					}
				}
		}
	}
	if(type==8){ // 向右看齐 
		for(k=1;k<MAX_HIGH_EX-1;k++){
			for(i=1;i<MAX_WIDTH_EX-1;i++)
				for(j=MAX_WIDTH_EX-2;j>i;j--){
					if(map[j][k]==0)
					{
						int temp = map[j][k];
						map[j][k]=map[j-1][k];
						map[j-1][k]=temp;
					}
				}
		}
	}
	if(type==9){ // ???

	}
	FILE *fp2=fopen("map1.txt","a+");//保存调整后的矩阵
	fprintf(fp2,"\n");
	for(j=0;j<MAX_HIGH_EX;j++){
		for(i=0;i<MAX_WIDTH_EX;i++){
			fprintf(fp2,"%.2d ",map[i][j]);
		}
		fprintf(fp2,"\n");
	}
	fclose(fp2);
}
int Find(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX])
{
	POINT p1={0};
	POINT p2={0};
	FILE *fpsol=fopen("map1.txt","a+");
	// 列出所有的选择方案
	for(int x1=0;x1<MAX_WIDTH_EX;x1++){
		for(int y1=0;y1<MAX_HIGH_EX;y1++){
			for(int x2=0;x2<MAX_WIDTH_EX;x2++){
				for(int y2=0;y2<MAX_HIGH_EX;y2++){
					p1.x=x1;
					p1.y=y1;
					p2.x=x2;
					p2.y=y2;
					if ((aMap[p1.x][p1.y]==0)||(aMap[p2.x][p2.y]==0) )continue;//不为0
					if(aMap[p1.x][p1.y]!=aMap[p2.x][p2.y])continue;//相等
					if((x1==x2)&&(y1==y2))continue;//同一个点
					if(testLine(aMap,&p1,&p2)){
						int temp=aMap[p1.x][p1.y];
						//PrintMap(aMap,&p1,&p2);
						aMap[p1.x][p1.y]=0;
						aMap[p2.x][p2.y]=0;
						move_map(map);


						fprintf(fpsol,"一直线 数值=%d  p1=%d,%d,p2=%d,%d\n",temp,p1.x,p1.y,p2.x,p2.y);
						fflush(fpsol);

						//cvRectangle( pImg, cvPoint((p1.x-1)*44+326,(p1.y-1)*40+218), cvPoint((p1.x-1)*44+326+44,(p1.y-1)*40+218+40), CV_RGB(255,0,0),4);
						//cvRectangle( pImg, cvPoint((p2.x-1)*44+326,(p2.y-1)*40+218), cvPoint((p2.x-1)*44+326+44,(p2.y-1)*40+218+40), CV_RGB(255,0,0),4);

						mouse_event((int)(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);

#ifdef PAUSE
						system("pause");
#endif
						continue;
						//fclose(fpsol);
						//return 1;
					}

					if(testcorner(aMap,&p1,&p2)){
						int temp=aMap[p1.x][p1.y];
						//PrintMap(aMap,&p1,&p2);
						aMap[p1.x][p1.y]=0;
						aMap[p2.x][p2.y]=0;
						move_map(map);
						fprintf(fpsol,"二直线 数值=%d  p1=%d,%d,p2=%d,%d\n",temp,p1.x,p1.y,p2.x,p2.y);
						fflush(fpsol);
						//cvRectangle( pImg, cvPoint((p1.x-1)*44+326,(p1.y-1)*40+218), cvPoint((p1.x-1)*44+326+44,(p1.y-1)*40+218+40), CV_RGB(255,0,0),4);
						//cvRectangle( pImg, cvPoint((p2.x-1)*44+326,(p2.y-1)*40+218), cvPoint((p2.x-1)*44+326+44,(p2.y-1)*40+218+40), CV_RGB(255,0,0),4);

						mouse_event((int)(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
#ifdef PAUSE
						system("pause");
#endif
						continue;
						//fclose(fpsol);
						//return 1;
					}
					if(testtwocorner(aMap,&p1,&p2)){
						int temp=aMap[p1.x][p1.y];
						//PrintMap(aMap,&p1,&p2);
						aMap[p1.x][p1.y]=0;
						aMap[p2.x][p2.y]=0;  
						move_map(map);


						fprintf(fpsol,"三直线 数值=%d  p1=%d,%d,p2=%d,%d\n",temp,p1.x,p1.y,p2.x,p2.y);
						fflush(fpsol);
						//cvRectangle( pImg, cvPoint((p1.x-1)*44+326,(p1.y-1)*40+218), cvPoint((p1.x-1)*44+326+44,(p1.y-1)*40+218+40), CV_RGB(255,0,0),4);
						//cvRectangle( pImg, cvPoint((p2.x-1)*44+326,(p2.y-1)*40+218), cvPoint((p2.x-1)*44+326+44,(p2.y-1)*40+218+40), CV_RGB(255,0,0),4);

						mouse_event((int)(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p1.x-1)*44+326+22), (65535/800)*((p1.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
						mouse_event((int)(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE), (65535/1280)*((p2.x-1)*44+326+22), (65535/800)*((p2.y-1)*40+218+20), 0, 0);
						Sleep(SLEEPTIME);
#ifdef PAUSE
						system("pause");
#endif
						continue;
						//fclose(fpsol);
						//return 1;
					}//*/
				}
			}
		}
	}
	fclose(fpsol);
	return 0;
}

bool testLine(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX],POINT *ap1,POINT* ap2)//测试两个点是不是能一条线连接
{
	if((ap1->x==ap2->x)||(ap1->y==ap2->y)){
		if(ap1->x==ap2->x){ //x轴相等
			int lisum=0;
			int min=0;
			int max=0;
			if(ap1->y > ap2->y){
				min=ap2->y;
				max=ap1->y;
			}else{
				max=ap2->y;
				min=ap1->y;    
			}
			for(int x=min+1;x<max;x++){
				if(aMap[ap1->x][x])return false;
			}
			return true;
		}
		if(ap1->y==ap2->y){ //x轴相等
			int lisum=0;
			int min=0;
			int max=0;
			if(ap1->x > ap2->x){
				min=ap2->x;
				max=ap1->x;
			}else{
				max=ap2->x;
				min=ap1->x;
			}
			for(int x=min+1;x<max;x++){
				if(aMap[x][ap2->y])return false;
			}
			return true;
		}
	}else{
		return false;
	}
	return false;
}
bool testcorner(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX],POINT *ap1,POINT* ap2)
{
	POINT tempPoint={0};
	for(int x=0;x<MAX_WIDTH_EX;x++){
		for(int y=0;y<MAX_HIGH_EX;y++){
			if(0!=aMap[x][y])
				continue;//不是空白
			tempPoint.x=x;
			tempPoint.y=y;
			if(testLine(aMap,ap1,&tempPoint)){
				if(testLine(aMap,&tempPoint,ap2)){
					//printf("tp %d,%d\n",x,y);
					return true;
				}
			}
		}
	}
	return false;
}
bool testtwocorner(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX],POINT *ap1,POINT* ap2)
{
	POINT tP1={0};
	POINT tP2={0};
	for(int x1=0;x1<MAX_WIDTH_EX;x1++){
		for(int y1=0;y1<MAX_HIGH_EX;y1++){
			for(int x2=0;x2<MAX_WIDTH_EX;x2++){
				for(int y2=0;y2<MAX_HIGH_EX;y2++){
					if(0!=aMap[x1][y1])
						continue;//不是空白
					if(0!=aMap[x2][y2])
						continue;//不是空白
					tP1.x=x1;
					tP1.y=y1;
					tP2.x=x2;
					tP2.y=y2;
					if(testLine(aMap,&tP1,&tP2)){
						if(testLine(aMap,&tP1,ap1)&&testLine(aMap,&tP2,ap2)){
							//printf("t1=%d,%d t2=%d,%d\n",x1,y1,x2,y2);
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}
bool testEmpty(int aMap[MAX_WIDTH_EX][MAX_HIGH_EX])
{
	int lisum=0;
	for(int x=0;x<MAX_WIDTH_EX;x++){
		for(int y=0;y<MAX_HIGH_EX;y++){
			lisum=lisum+aMap[x][y];
		}
	}
	if (0==lisum)
		return true;
	return false;
}

void gen_map()
{
	IplImage*  pImg,* pdiff,*temp1; //声明IplImage指针
	char * filename = "cwllknm.bmp";
	IplImage* partImg[MAX_WIDTH][MAX_HIGH];
	//载入图像
	if( (pImg = cvLoadImage( filename, 1)) != 0 )
	{
		int i,j;
		for(i = 0; i<MAX_WIDTH;i++)
			for(j=0; j<MAX_HIGH;j++)
			{
				cvSetImageROI( pImg, cvRect(326+44*i,218+40*j,44,40 ));//初始化小图片矩阵。 326,218是左上角坐标。44,40小图的宽和高
				partImg[i][j]=cvCloneImage(pImg);
				cvResetImageROI( pImg);
			}
			cvReleaseImage(&partImg[i-1][j-1]);
			cvSetImageROI( pImg, cvRect(326+44*(i-1),218+1+40*(j-1),44,40 )); //右下角的图下移了一个像素，so 218+1
			partImg[i-1][j-1]=cvCloneImage(pImg);
			cvResetImageROI( pImg);

			pdiff = cvCreateImage(cvGetSize(partImg[0][0]),partImg[0][0]->depth,partImg[0][0]->nChannels); //存差值
			temp1 = cvCreateImage(cvGetSize(partImg[0][0]),partImg[0][0]->depth,1); 
			printf("%d %d\n",partImg[0][0]->width, partImg[0][0]->height);

			int c=10;
			int q,p;
			int a=0;
			FILE *fdiffp =fopen("fdiff.txt","w"); //writable
			for(i = 0; i<MAX_WIDTH;i++)
				for(j=0; j<MAX_HIGH;j++) 
				{
					long sum=0;
					cvCvtColor(partImg[i][j],temp1,CV_BGR2GRAY); //转灰度
					for(int m=0;m<temp1->width;m++)
						for(int n=0;n<temp1->height;n++){
							sum+=(unsigned char)temp1->imageData[m*temp1->width+n];
						}
						int avrgrey=(int)(sum/(temp1->width*temp1->height));
						fprintf(fdiffp,"%d %d %d\n",i,j,avrgrey);
						if(avrgrey<100) //背景为黑色时，为空区域，置零
						{
							b[i][j]=0;
							continue;
						}
						p = i;
						if(b[i][j]==0){ //没和前面的匹配过
							c++; //id#
							b[i][j]=c;
							for(q = j+1; q<MAX_HIGH;q++) //b[i][j+1..末尾]
							{
								a = 0;//cvCountNonZero(img3);
								cvAbsDiff(partImg[i][j],partImg[p][q],pdiff);
								cvCvtColor(pdiff,temp1,CV_BGR2GRAY);
								a=cvCountNonZero(temp1);//差值，0越多越匹配
								fprintf(fdiffp,"%d %d %d %d %d %d\n",i,j,p,q,a,c);
								if(a<100) // matched
								{
									fprintf(fdiffp,"match!\n");
									b[p][q]=c;
								}
							}	

							for(p=i+1; p<MAX_WIDTH;p++){//b[i+1..末尾][j+1..末尾]
								for(q=0; q<MAX_HIGH;q++) 
								{
									a = 0;//cvCountNonZero(img3);
									cvAbsDiff(partImg[i][j],partImg[p][q],pdiff);
									cvCvtColor(pdiff,temp1,CV_BGR2GRAY);
									a=cvCountNonZero(temp1);
									fprintf(fdiffp,"%d %d %d %d %d %d\n",i,j,p,q,a,c);
									if(a<100) // matched
									{
										fprintf(fdiffp,"match!\n");
										b[p][q]=c;
									}
								}			
							}
						}//if(b[][])
				}
				FILE *fp =fopen("map.txt","w");
				if(fp !=NULL){
					for(j=0;j<MAX_HIGH;j++){
						for(i=0;i<MAX_WIDTH;i++){
							fprintf(fp,"%d ",b[i][j]);
						}
						fprintf(fp,"\n");
					}
				}
				fclose(fp);
				for(i=0+1;i<MAX_WIDTH+1;i++) //转成外围包一层0的矩阵
					for(j=0+1;j<MAX_HIGH+1;j++){
						map[i][j]=b[i-1][j-1];
					}

			FILE *fp1 =fopen("map1.txt","a+");
			if(fp1 !=NULL){
				for(j=0;j<MAX_HIGH_EX;j++){
					for(i=0;i<MAX_WIDTH_EX;i++){
						fprintf(fp1,"%.2d ",map[i][j]);
					}
					fprintf(fp1,"\n");
				}
			}
			fprintf(fp1,"\n");
			fclose(fp1);
			cvReleaseImage( &pImg ); //释放图像
			 cvReleaseImage(&pdiff);
			 cvReleaseImage(&temp1);
			 for(i = 0; i<MAX_WIDTH;i++)
				 for(j=0; j<MAX_HIGH;j++)
				 {
					 
					 cvReleaseImage(&partImg[i][j]);
					 
				 }
		}//if
}
int main( int argc, char** argv )
{

	//IplImage* pImg;
	
	Sleep(3000);//延时3s
 	screen(filename);
 	gen_map();
	
	int x=0;
	type =0;
	while (1) {
		
		if(Find(map)){
			Sleep(1000);
			continue;
		}
		if(testEmpty(map))
			break;
		if(x>30){
			printf("无解，你运气真好！\n");
			break;
		}
		printf("new Find\n");
		x++;
	}

}